import java.util.Scanner;

/** 

 @author Paul Smelser This is the main file that uses everything together.
 The reason for this file's existance is to provide the user with a proper 
 interface to work with.

**/

public class Main{

  public static void main(String[] args){

    int i; // looping
    String str = new String();
    // System.out.println("Loading......");
    
    Tokenizer tok = new Tokenizer();
	SpimiInvert invert=new SpimiInvert();
	
    for (i=0; i<args.length; i++){
      System.out.print("Loading " + args[i]);
      System.out.print("\t[BUSY]"); 
      tok.TokenizeFile(args[i]); 
      

      invert.spimiInvert(tok.getTokens(), args[i]);
      tok.emptyTokens(); 
      System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
      System.out.print("\t[DONE]\n");
      
    }
      /**
      *This section contains all the user interface actions
      *
      */
      Scanner kb   = new Scanner(System.in);
      String terms="";
      String[] term=new String[25];
      term[0]="initialize";
      PostingsList[] postings=new PostingsList[25];
      System.out.println("\nWelcome. Enter 'quittheprogram' to terminate\n");

      while(!term[0].equalsIgnoreCase("quittheprogram")){
        System.out.print("query> ");
        terms = terms+kb.nextLine().toLowerCase();
		term = terms.split("\\s");
        try{
        for(int j=0; j<term.length; i++){
        	postings[j]=invert.getDictionary().theList(term[j]);
          //System.out.println("Results for "+term[i]+"---> "+invert.getDictionary().theList(term[i]).displayContents());
        }
        PostingsList test=postings[0].mergeTwoLists(postings[1]);
        System.out.println("Results for "+terms+" ---> "+test.displayContents());
        } 
	catch (NullPointerException e) {
			if(term[0].equalsIgnoreCase("quittheprogram"))
			{
			System.out.println("Goodbye!!");
			}
			else
			{
			System.out.println("Sorry, your query is not in our files!");
			}
		}
      }
	//end of user interface------------------------------------------------

  }

}
